import { Component, OnInit } from '@angular/core';
import {UserService} from "../user.service";
import {Response} from "@angular/http";
import {ActivatedRoute} from "@angular/router";

@Component({
  selector: 'app-user-forget',
  templateUrl: './user-forget.component.html',
  styleUrls: ['./user-forget.component.css']
})
export class UserForgetComponent implements OnInit {

  constructor(
    private userService:UserService,
    private activatedRoute:ActivatedRoute,
  ) { }

  forgetInfo:ForgetInfo = new ForgetInfo();
  resetInfo:ResetInfo = new ResetInfo();

  ngOnInit() {
    let id = this.activatedRoute.snapshot.params['id'];
    console.log("id:");
    console.log(id)
    this.userService.findForgetInfoById(id)
      .subscribe((res:Response)=>{
        let data = res.json();
        if (data.success){
          console.log("忘记密码返回的数据:");
          console.log(data);
          this.forgetInfo = data.value;
        }
      });
  }

  resetPw(){
    let old = this.resetInfo.password;
    let now = this.resetInfo.confirmPassword;
    let accountInfo = {code:this.forgetInfo.code,password:old};
    if (old == now){
      this.userService.resetPassword(accountInfo)
        .subscribe((res:Response)=>{
          let data = res.json();
          console.log(data);
        });
    }
  }

}
export class ForgetInfo{
  code:string;
  createTime:string;
  creator:string;
  dataStatus:number;
  id:string;
  name:string;
  operType:string;
  properties:any;
  status:number;
  statusName:string;
  updateTime:string;
  updator:string;
}
export class ResetInfo{
  email:string;
  password:string;
  confirmPassword:string;
}
